package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.ruoyi.system.domain.LineDevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.LineProductionMapper;
import com.ruoyi.system.domain.LineProduction;
import com.ruoyi.system.service.ILineProductionService;

import com.ruoyi.system.mapper.LineDeviceMapper;


/**
 * 产线管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-12
 */
@Service
public class LineProductionServiceImpl implements ILineProductionService 
{
    @Autowired
    private LineProductionMapper lineProductionMapper;

    @Autowired
    private LineDeviceMapper lineDeviceMapper;


    /**
     * 查询产线管理
     * 
     * @param id 产线管理主键
     * @return 产线管理
     */
    @Override
    public LineProduction selectLineProductionById(Integer id)
    {
        return lineProductionMapper.selectLineProductionById(id);
    }

    /**
     * 查询产线管理列表
     * 
     * @param lineProduction 产线管理
     * @return 产线管理
     */
    @Override
    public List<LineProduction> selectLineProductionList(LineProduction lineProduction)
    {
        return lineProductionMapper.selectLineProductionList(lineProduction);
    }

    /**
     * 新增产线管理（重复产线不得被添加）
     * 
     * @param lineProduction 产线管理
     * @return 结果
     */
    @Override
    public int insertLineProduction(LineProduction lineProduction)
    {
        List<LineProduction> lineProductions = selectLineProductionList(lineProduction);
        if(lineProductions.isEmpty()){
            return lineProductionMapper.insertLineProduction(lineProduction);
        }
        return 0;
    }

    /**
     * 修改产线管理
     * 
     * @param lineProduction 产线管理
     * @return 结果
     */
    @Override
    public int updateLineProduction(LineProduction lineProduction)
    {
        return lineProductionMapper.updateLineProduction(lineProduction);
    }

    /**
     * 批量删除产线管理（非空产线不得被删除）
     * 
     * @param ids 需要删除的产线管理主键
     * @return 结果
     */
    @Override
    public int deleteLineProductionByIds(Integer[] ids)
    {
        LineDevice lineDevice = new LineDevice();
        int i = 0;
//        lineDevice.setDelFlag(1);
        for (Integer id : ids){
            i++;
            lineDevice.setProductionId(id);
            if(!Objects.equals
                    (lineDeviceMapper.selectLineProductionDevicesByLineDevice(lineDevice), new ArrayList<>())){
                continue;
            }
            if(ids.length == i){
                return lineProductionMapper.deleteLineProductionById(id);
            }
            lineProductionMapper.deleteLineProductionById(id);
        }
        return 0;
    }
//    @Override
//    public int deleteLineProductionByIds(Integer[] ids)
//    {
////        LineDevice lineDevice = new LineDevice();
////        lineDevice.setDelFlag(1);
////        for (Integer id : ids){
////            lineDevice.setProductionId(id);
////            lineDeviceMapper.deleteLineDeviceByLineDevice(lineDevice);
////        }
//        return lineProductionMapper.deleteLineProductionByIds(ids);
//    }

    /**
     * 删除产线管理信息
     * 
     * @param id 产线管理主键
     * @return 结果
     */
    @Override
    public int deleteLineProductionById(Integer id)
    {
        return lineProductionMapper.deleteLineProductionById(id);
    }
}
